<?php

session_start();
if(!array_key_exists("user", $_SESSION) || $_SESSION['account'] != 2){
	header("Location: ../common/redirect.php");
	exit;
}

include_once("../class/db.php");
$db = new db();
if(array_key_exists("contenttype", $_POST)){
	$uid = $_POST['uid'];
	$sql = "SELECT MAX(order_number) as number FROM course_content WHERE uid = $uid and owner = {$_SESSION['user']}";
	$course_content_order_number = $db->execute($sql, "onerow");

	if($course_content_order_number['number'] == NULL){
		$course_order_number_new = 1;
	} else {
		$course_order_number_new = $course_content_order_number['number'] + 1;
	}

	$title = addslashes(preg_replace("/[^a-zA-Z0-9\.\'\"\s]/", "", $_POST['title']));
	$content = addslashes($_POST['homework_content']);
	$deliver_method = $_POST['deliver_method'];
	$end_date1 = explode(" ",  preg_replace("/[^a-zA-Z0-9_\-:\.\s]/", "", $_POST['end_date']));
	$end_date2 = explode("-", $end_date1[0]);
	$end_hour = explode(":", $end_date1[1]);
	$end_date = date("Y-m-d H:i:s", mktime($end_hour[0], $end_hour[1], $end_hour[2], $end_date2[1], $end_date2[0], $end_date2[2]));
	
	$sql = "INSERT INTO course_content (uid, owner, order_number, title, contenttype, content, deliver_method, homework_end_date) VALUES ($uid, {$_SESSION['user']}, $course_order_number_new, '$title', 'homework', '$content', '$deliver_method', '$end_date')";
	$db->execute($sql, "none");
	$newhomework = true;
}

if(array_key_exists("action", $_GET) && $_GET['action'] == "delete"){
	$id = preg_replace("/[^a-zA-Z0-9\s]/", "", $_GET['id']);
	$sql = "DELETE FROM course_content WHERE owner = {$_SESSION['user']} AND contenttype = 'homework' AND id = $id";
	$db->execute($sql, "none");
	$deleted = true;
}

$sql = "SELECT id, title, homework_end_date, deliver_method FROM course_content WHERE owner = {$_SESSION['user']} AND contenttype = 'homework'";
$data = $db->execute($sql, "all");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PIE EDUCATION | Educational Network</title>
<link rel="stylesheet" type="text/css" href="/css/style-student.css" />
<script type="text/javascript" src="/js/index.js"></script>
<script language="Javascript1.2">
<!--
setTimeout('location.href="/logout.php?expire=1"',600000);
// -->
</script>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-35230372-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</head>

<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td bgcolor="#000000"><table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td width="350" height="30">&nbsp;</td>
        <td align="right" valign="bottom"><a href="class.php" class="top-menu" style="color:#999">MY CLASS</a> <a href="schedule.php" class="top-menu" style="color:#999">CALENDAR</a> <a href="homework.php" class="top-menu" style="color:#999">HOMEWORK</a> <a href="test.php" class="top-menu" style="color:#999">EXAMS</a> <a href="grades.php" class="top-menu" style="color:#999">GRADES</a> <a href="tools.php" class="top-menu" style="color:#999">TOOLS</a> <a href="/logout.php" class="top-menu" style="color:#999">SIGN OUT</a></td>
      </tr>
	  <tr>
        <td>&nbsp;</td>
        <td align="right" valign="bottom"><a href="/profile/" class="login-menu" style="color:#FFF"><?php echo $_SESSION['name'].' '.$_SESSION['lname']; ?></a> <a href="/common/redirect.php" class="login-menu" style="color:#FFF">My Documents</a> <a href="/network/" class="login-menu" style="color:#FFF">My Wall</a> <a href="/common/settings.php" class="login-menu" style="color:#FFF">Settings</a></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="30" style="background-image:url(/images/top-bg-student.png); background-repeat:repeat-x">&nbsp;</td>
  </tr>
  <tr>
    <td height="550" align="center" valign="top"><table width="980" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><h1>Active Homeworks</h1></td>
        <td width="350" align="right"><input type="button" class="submit-course" onclick="location.href='homework-create.php'" value='Create a New Homework' tabindex="10" /></td>
      </tr>
      <?php
	  if(isset($newhomework)){
      echo '<tr>
        <td colspan="2" height="30" align="center" style="color:#F00;font-family:Arial, Helvetica, sans-serif"><strong>The homework has been saved correctly</strong></td>
        </tr>';
	  } else if(isset($deleted)){
		  echo '<tr>
        <td colspan="2" height="30" align="center" style="color:#F00;font-family:Arial, Helvetica, sans-serif"><strong>The homework has been deleted</strong></td>
        </tr>';
	  } else if(array_key_exists("action", $_GET) && $_GET['action'] == "updated"){
		  echo '<tr>
        <td colspan="2" height="30" align="center" style="color:#F00;font-family:Arial, Helvetica, sans-serif"><strong>The homework has been updated correctly</strong></td>
        </tr>';
	  }
        ?>
      <tr>
        <td colspan="2">
        
        <table width="100%" border="0" cellspacing="1" cellpadding="0">
          <?php
		  	if($data != false){
		  ?>
          <tr>
            <td width="40%" height="30" align="center" bgcolor="#cccccc" style="font-family:Arial, Helvetica, sans-serif; font-size:14px"><strong>Title</strong></td>
            <td width="20%" align="center" bgcolor="#cccccc" style="font-family:Arial, Helvetica, sans-serif; font-size:14px"><strong>Deliver Method</strong></td>
            <td width="20%" align="center" bgcolor="#cccccc" style="font-family:Arial, Helvetica, sans-serif; font-size:14px"><strong>Deliver Date</strong></td>
            <td width="10%" align="center" bgcolor="#cccccc" style="font-family:Arial, Helvetica, sans-serif; font-size:14px"><strong>Grade</strong></td>
            <td width="10%" align="center" bgcolor="#cccccc" style="font-family:Arial, Helvetica, sans-serif; font-size:14px"><strong>Delete</strong></td>
          </tr>
          <?php
			  $counter = 1;
			  foreach($data as $row){
				  if($counter % 2){
					  
				  }
				  echo '<tr>
					<td style="font-family:Arial, Helvetica, sans-serif; font-size:12px" height="30"><a href="homework-edit.php?id='.$row['id'].'">'.$row['title'].'</a></td>
					<td align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$row['deliver_method'].'</td>
					<td align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$row['homework_end_date'].'</td>
					<td align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:12px"><a href="homework-review.php?id='.$row['id'].'">Review</a></td>
					<td align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:12px"><a href="homework.php?action=delete&id='.$row['id'].'" onclick="return confirm(\'Are you sure you want to delete this Homework?\')">Delete</a></td>
				  </tr>';
			  }
		  } else {
			  echo '<tr>
					<td align="center" colspan="5" height="80"><div style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#666666;"><strong>No Homeworks Found</strong></div><div style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#999999;">Create a new homework using the button above</div></td>
				  </tr>';
		  }
          ?>
        </table></td>
      </tr>
</table></td>
  </tr>
  <tr>
    <td height="30" align="center" style=" background-image:url(../images/bottom-bg-student.png); background-repeat:repeat-x;">&nbsp;</td>
  </tr>
  <tr>
    <td height="50" align="center" bgcolor="#000000" style="color:#fff; font-family:Arial, Helvetica, sans-serif">PIE.EDU All Rights Reserved 2012<br />
      <a href="/common/privacy.php" class="bottom-menu" style="color:#999">Privacy Policy</a> | <a href="/common/terms.php" class="bottom-menu"  style="color:#999">Terms of Use</a> | <a href="/help/main.php" class="bottom-menu" style="color:#999">Help Center</a> | <a href="/common/jobs.php" class="bottom-menu" style="color:#999">Jobs</a></td>
  </tr>
    </table>
</body>
</html>